package cn.edu.xmu.oomall.customer.dao;


import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.customer.mapper.ShoppingCartPoMapper;
import cn.edu.xmu.oomall.customer.microservice.vo.ProductRetVo;
import cn.edu.xmu.oomall.customer.microservice.vo.ProductVoInfo;
import cn.edu.xmu.privilegegateway.annotation.util.Common;
import cn.edu.xmu.oomall.customer.model.po.ShoppingCartPo;
import cn.edu.xmu.oomall.customer.model.po.ShoppingCartPoExample;
import cn.edu.xmu.oomall.customer.model.vo.SimpleCartItemVo;
import cn.edu.xmu.privilegegateway.annotation.util.RedisUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;

/**
 * @author Haotian Cheng
 * @date 2021-12-14 13:06:13
 * @modifiedBy Jiazhe Yuan
 * @date 2021-12-16 10:51:19
 * @modified deleteCartsById方法，原先将id用作productId使用，改为将id作为cartId使用
 */
@Repository
public class CartDao {
    @Autowired(required = false)
    ShoppingCartPoMapper shoppingCartPoMapper;

    @Autowired
    private RedisUtil redisUtil;

    public PageInfo<ShoppingCartPo> getCarts(Long customerId) {
        ShoppingCartPoExample shoppingCartPoExample = new ShoppingCartPoExample();
        ShoppingCartPoExample.Criteria criteria = shoppingCartPoExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        List<ShoppingCartPo> shoppingCartPos = shoppingCartPoMapper.selectByExample(shoppingCartPoExample);
        return new PageInfo<>(shoppingCartPos);
    }

    public ReturnObject<List<ShoppingCartPo>> getCartByPidAndCid(Long pid,Long cid) {
        ShoppingCartPoExample shoppingCartPoExample = new ShoppingCartPoExample();
        ShoppingCartPoExample.Criteria criteria = shoppingCartPoExample.createCriteria();
        criteria.andProductIdEqualTo(pid);
        criteria.andCustomerIdEqualTo(cid);
        List<ShoppingCartPo> shoppingCartPos = shoppingCartPoMapper.selectByExample(shoppingCartPoExample);
        return new ReturnObject<>(shoppingCartPos);
    }

    public ReturnObject createNewCartItem(Long customerId, String customerName, ProductRetVo product, Long quantity) {
        ShoppingCartPo shoppingCartPo=new ShoppingCartPo();
        shoppingCartPo.setCustomerId(customerId);
        shoppingCartPo.setProductId(product.getId());
        shoppingCartPo.setPrice(product.getPrice());
        shoppingCartPo.setQuantity(quantity);

        Common.setPoCreatedFields(shoppingCartPo, customerId, customerName);
        try {
            shoppingCartPoMapper.insertSelective(shoppingCartPo);
        } catch(Exception e) {
            System.out.println("in dao createNewCartItem");
            System.out.println(e.getMessage());
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
        return new ReturnObject<>(shoppingCartPo);
    }

    public ReturnObject deleteCarts(Long customerId) {
        ShoppingCartPoExample shoppingCartPoExample = new ShoppingCartPoExample();
        ShoppingCartPoExample.Criteria criteria = shoppingCartPoExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);

        try {
            shoppingCartPoMapper.deleteByExample(shoppingCartPoExample);
        } catch(Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }

        return new ReturnObject<>(ReturnNo.OK);
    }


    public  ReturnObject updateProductInCartsById(ShoppingCartPo shoppingCartPo) {
        try {
            shoppingCartPoMapper.updateByPrimaryKeySelective(shoppingCartPo);
        } catch(Exception e) {
            System.out.println("in dao updateProductInCartsById");
            System.out.println(e.getMessage());
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
        return new ReturnObject<>(ReturnNo.OK);
    }


    public ReturnObject deleteCartsById(Long customerId,Long id) {
        ShoppingCartPoExample shoppingCartPoExample = new ShoppingCartPoExample();
        ShoppingCartPoExample.Criteria criteria = shoppingCartPoExample.createCriteria();
        criteria.andCustomerIdEqualTo(customerId);
        criteria.andIdEqualTo(id);

        try {
            shoppingCartPoMapper.deleteByExample(shoppingCartPoExample);
        } catch(Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }

        return new ReturnObject<>(ReturnNo.OK);
    }
}